package com.epfresh.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.epfresh.BuildConfig;
import com.epfresh.R;
import com.epfresh.api.constant.Constant;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.api.utils.ToastUtils;
import com.epfresh.bean.StoreDetail;
import com.epfresh.global.BaseActivity;
import com.epfresh.global.BaseApplication;
import com.epfresh.global.MessageCentre;
import com.epfresh.utils.CommonUtils;
import com.epfresh.utils.DataCleanManager;
import com.google.gson.Gson;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class StoreDetailActivity extends BaseActivity {
    private TextView tvMarket;
    private TextView tvAddress;
    private TextView tvPhone;
    private TextView tvPayment;
    private TextView tvDiscount;
    private TextView tvDelivery;
    //private TextView tvCert;
    private TextView tvRate;
    private TextView tvAmount;
    private TextView tvAcceptTime;
    private TextView tvDiscountLevel;
    private TextView tvDiscountDetail;
    private TextView tvCategory;
    private TextView tvDetail;
    private View llRate;


    private StoreDetail storeDetail;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_store_info);

        if (savedInstanceState != null){
            getIntent().putExtra("store_detail",savedInstanceState.getString("store_detail"));
        }
        String sd = getIntent().getStringExtra("store_detail");
        storeDetail = new Gson().fromJson(sd,StoreDetail.class);

        initView();

    }

    private void initView() {
        ivBack.setVisibility(View.VISIBLE);
        basetitle.setText("店铺信息");


        tvMarket = (TextView) findViewById(R.id.tv_market);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvPayment = (TextView) findViewById(R.id.tv_payment);
        tvDiscount = (TextView) findViewById(R.id.tv_discount);
        tvDelivery = (TextView) findViewById(R.id.tv_delivery);
        //tvCert = (TextView) findViewById(R.id.tv_cert_status);
        tvRate = (TextView) findViewById(R.id.tv_rate);
        tvAmount = (TextView) findViewById(R.id.tv_amount);
        tvAcceptTime = (TextView) findViewById(R.id.tv_accept_time);
        llRate = findViewById(R.id.ll_comment);
       findViewById(R.id.iv_rate).setOnClickListener(this);
        findViewById(R.id.tv_rate).setOnClickListener(this);

//        llRate.setOnClickListener(this);

        findViewById(R.id.ll_phone).setOnClickListener(this);


        tvDiscountLevel = (TextView) findViewById(R.id.tv_discount_level);
        tvDiscountDetail = (TextView) findViewById(R.id.tv_discount_detail);
        tvCategory = (TextView) findViewById(R.id.tv_category);
        tvDetail = (TextView) findViewById(R.id.tv_detail);

        updateStoreStatus();
    }



    public void updateData(StoreDetail storeDetail) {
        this.storeDetail = storeDetail;
        updateStoreStatus();

    }


    public void updateStoreStatus() {
        if (tvMarket != null) {
            if (storeDetail != null) {
                tvDiscountLevel.setVisibility(View.VISIBLE);
                tvDiscountDetail.setVisibility(View.VISIBLE);
                tvDiscountDetail.setText(storeDetail.getDiscountInfo());
                if(storeDetail.getDiscountInfo()==null||"".equals(storeDetail.getDiscountInfo())){
                    findViewById(R.id.rl_discount).setVisibility(View.GONE);
                }
                tvMarket.setText(storeDetail.getMarketName());
                tvAddress.setText(storeDetail.getAddress());
                tvPhone.setText(storeDetail.getManagerPhone());
                tvPayment.setText(storeDetail.getPaymentInfo());
                tvDiscount.setText(storeDetail.getDiscount());
                tvDelivery.setText(storeDetail.getDeliveryInfo());
                //tvCert.setText(storeDetail.getCert());
                tvAmount.setText(storeDetail.getOrderCount() + "单");
                tvRate.setText(storeDetail.getGoodCommentsRate() + "好评率");
                String businessTime = storeDetail.getBusinessTime();
                if (businessTime == null || "".equals(businessTime)){
                    tvAcceptTime.setText("00:00-24:00");
                }else {

                    tvAcceptTime.setText(businessTime);
                }
                tvCategory.setText(storeDetail.getMainBusinessCategoryNames());
                tvDetail.setText(storeDetail.getDescription());
            } else {
                tvMarket.setText("");
                tvAddress.setText("");
                tvPhone.setText("");
                tvPayment.setText("");
                tvDiscount.setText("");
                tvDelivery.setText("");
                //tvCert.setText("");
                tvAmount.setText("");
                tvRate.setText("");
                tvDiscountLevel.setVisibility(View.GONE);
                tvDiscountDetail.setVisibility(View.GONE);
                tvDiscountLevel.setText("");
                tvDiscountDetail.setText("");
                tvCategory.setText("");
                tvDetail.setText("");
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_more_dc:
//                intoMoreDc();

                break;
            case R.id.iv_rate:
            case R.id.tv_rate:
                intoComment();

                break;
            case R.id.ll_phone:
                String managerPhone = storeDetail.getManagerPhone();
                if (managerPhone != null && !"".equals(managerPhone))
                    CommonUtils.intoPhone(this, managerPhone);

                break;
        }
    }

    private void intoComment() {
        Intent more = new Intent(this, MoreEvaluateActivity.class);
        if (storeDetail == null) {
            T.toast("数据已过期，请重载应用");
        }
        more.putExtra("evaluate_percent", storeDetail.getGoodCommentsRate());
        more.putExtra("storeId", storeDetail.getId());
        this.startActivity(more);
    }



}
